package com.ruoyi.web.controller.system;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.framework.shiro.service.SysPasswordService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.shiro.service.SysRegisterService;
import com.ruoyi.system.service.ISysConfigService;

import java.util.UUID;

/**
 * 注册验证
 * 
 * @author ruoyi
 */
@Controller
public class SysRegisterController extends BaseController
{


    @Autowired
    private ISysUserService userService;

    @Autowired
    private SysPasswordService passwordService;

    @Autowired
    private SysRegisterService registerService;

    @Autowired
    private ISysConfigService configService;

    @GetMapping("/register")
    public String register()
    {
        return "register";
    }

    @GetMapping("/resetPwd")
    public String resetPwd()
    {
        return "resetPwd";
    }

    public static String generateFixedLengthUUID(int length) {
        if (length < 1 || length > 36) { // UUID最长36位
            throw new IllegalArgumentException("Length must be between 1 and 36.");
        }
        UUID uuid = UUID.randomUUID();
        String uuidStr = uuid.toString().replace("-", ""); // 移除破折号
        return uuidStr.substring(0, length); // 截取指定长度
    }

    @PostMapping("/register")
    @ResponseBody
    public AjaxResult ajaxRegister(String loginName, String oldPassword,  String newPassword)
    {
//        if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser"))))
//        {
//            return error("当前系统没有开启注册功能！");
//        }
 //       String msg = registerService.register(user);
 //       return StringUtils.isEmpty(msg) ? success() : error(msg);

        SysUser user = userService.selectUserByLoginName(loginName);
        System.out.println("======================salt===========:"+user.getSalt());
        System.out.println("=================================:"+oldPassword);
        if (!passwordService.matches(user, oldPassword))
        {
            return error("修改密码失败，旧密码错误");
        }
        if (passwordService.matches(user, newPassword))
        {
            return error("新密码不能与旧密码相同");
        }
        user.setSalt(generateFixedLengthUUID(6));
      //  user.setSalt(ShiroUtils.randomSalt());
        user.setPassword(passwordService.encryptPassword(user.getLoginName(), newPassword, user.getSalt()));
        user.setPwdUpdateDate(DateUtils.getNowDate());
        if (userService.resetUserPwd(user) > 0)
        {
           // setSysUser(userService.selectUserById(user.getUserId()));
            return success("修改密码成功");
        }
        return error("修改密码异常，请联系管理员");
    }
}
